// 封装request
// 配置请求和响应拦截器

import axios from "axios";
import { getToken } from '@/utils/auth'


const request = axios.create({
    timeout: 5000,
    headers: { 'Content-Type': 'application/json;charset=utf-8' },
    baseURL: "http://127.0.0.1:8888"
})

// request拦截器
request.interceptors.request.use(
    /*     config.headers = config.headers || {}
        console.log(config);
        return config */
    config => {
        if (getToken()) {
            config.headers['token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
        }
        config.headers['Content-Type'] = 'application/json'
        return config
    },
    error => {
        Promise.reject(error)
    })

// response拦截器
request.interceptors.response.use(res => {
    const code = res.data?.code;
    if (code !== 200) {
        return Promise.reject('您的请求被拒绝！')
    } else {
        return res.data
    }
}, error => {
    console.log(error)
})

export default request